VFG.DrawVModel=function(viewer,option){
	this.viewer=viewer;
	this.option=option;
	this.id=option.id;
	this.model=option.thing;
	this.position=null;
	this.tip=new VFG.Tip(viewer);
	this.loading=false;
	this.loadingError=true;
	this.entity;
	this.init();
}

VFG.DrawVModel.prototype.init = function() {
	var _this=this;
	this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
	this.handler.setInputAction(function (movement) {
		if(_this.success && !_this.loadingError){
			_this.success(_this.position);
		}else{
			if(_this.error){
				_this.error();
			}
		}
		_this.destroy();
    },Cesium.ScreenSpaceEventType.LEFT_CLICK)
    
    this.handler.setInputAction(function (movement) {
    	if(_this.loading){
    		_this.tip.showAt(movement.endPosition,"加载模型中...");
    	}else{
    		if(_this.loadingError){
    			_this.tip.showAt(movement.endPosition,"模型加载失败，右键退出");
    		}else{
    			_this.tip.showAt(movement.endPosition,"单击完成绘制，右键取消！");
    		}
    	}
    	let position=VFG.Util.getScreenToLnLa(_this.viewer, movement.endPosition, _this.entity);
    	if (Cesium.defined(position)){
    		_this.position=position;
    		_this.render(position);
    	}
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	this.handler.setInputAction(function (movement) {
		if (Cesium.defined(_this.entity)){
			_this.entity.destroy();
		}
		if(_this.error){
			_this.error();
		}
		_this.destroy();
	}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
};

VFG.DrawVModel.prototype.render = function(position) {
	var _this=this;
	if (!Cesium.defined(this.entity) && this.loading==false){
		this.loading=true;
		this.option.position=position;
		this.entity=VFG.Render.getVModel(_this.viewer,this.option,this.option.uri);
		this.entity.addListener("beforeSend",function(e){
		});
		this.entity.addListener("success",function(e){
			_this.loading=false;
			_this.loadingError= false;
			console.log(e);
		});
		this.entity.addListener("error",function(e){
			_this.loading=false;
			_this.loadingError=true;
			console.log(e);
		});
		this.entity.addListener("complete",function(e){
		});
	}
	if (Cesium.defined(this.entity) && this.loading==false && !this.loadingError){
		this.option.position=position;
		this.entity.update({
			position:position
		});
	}
};

VFG.DrawVModel.prototype.on = function(type,callback) {
	var _this=this;
	if("error"==type){
		this.error=callback;
	}
	if("success"==type){
		this.success=callback;
	}
	if("complete"==type){
		this.complete=callback;
	}
};

VFG.DrawVModel.prototype.destroy = function() {
	if(this.complete){
		this.complete();
	}
    if( this.handler){
    	this.handler.destroy();
    	this.handler = null;
    };
    if(this.tip){
    	this.tip.destroy();
    }
	if(this.entity){
		this.entity.destroy();
	}
	delete this.entity;
    delete this.viewer;
	return Cesium.destroyObject(this);
};
